package com.dashu.device.netty.ipml;

import com.alibaba.fastjson.JSONObject;
import com.dashu.base.bean.device.HF.req.*;
import com.dashu.base.enums.device.ModuleType;
import com.dashu.base.utils.ToEntityAnStr;
import com.dashu.device.api.HF.HFInsideServerApi;
import com.dashu.device.service.HF.HFnsideInsideService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;

import static com.dashu.device.tools.HFMark.*;
@Slf4j
public class HFServerImpl {


    private HFInsideServerApi hfServerApi = new HFnsideInsideService();

    public HFServerImpl(String msg, ChannelHandlerContext ctx) {
        try {
            JSONObject baseReq = ToEntityAnStr.strToJSON(msg);
            log.info(ToEntityAnStr.obj2String(baseReq));
            //不处理不带sn设备
            if (baseReq == null || TextUtils.isEmpty(baseReq.getString("request_type")))
                return;
            switch (baseReq.getString("request_type")) {
                case HF_REQ_HEARD:
                    HFReqHeartbeat hfReqHeartbeat = ToEntityAnStr.string2Obj(msg, HFReqHeartbeat.class);
                    hfServerApi.heartbeat(ctx, hfReqHeartbeat, 0);
                    break;
                case HF_REQ_ONLINE:
                    HFReqOnline reqOnline = ToEntityAnStr.string2Obj(msg, HFReqOnline.class);
                    hfServerApi.register(ctx, reqOnline);
                    break;
                case HF_REQ_COMPARED:
                    HFReqCompared reqCompared = ToEntityAnStr.string2Obj(msg, HFReqCompared.class);
                    hfServerApi.compared(ctx, reqCompared, msg);
                    break;
                case HF_REQ_ADD:
                    HFReqAddOrUpUser hfReqAddUser = ToEntityAnStr.string2Obj(msg, HFReqAddOrUpUser.class);
                    break;
                case HF_REQ_DEL:
                    HFReqDelUser hfReqDelUser = ToEntityAnStr.string2Obj(msg, HFReqDelUser.class);
                    break;
                case HF_REQ_UP:
                    HFReqAddOrUpUser hfReqUpUser1 = ToEntityAnStr.string2Obj(msg, HFReqAddOrUpUser.class);
                    break;
                case HF_REQ_OPEN_DOOR:
                    HFReqOpenDoor hfReqOpenDoor = ToEntityAnStr.string2Obj(msg, HFReqOpenDoor.class);
                    break;
                case HF_REQ_RESTART:
                    HFReqRestart hfReqRestart = ToEntityAnStr.string2Obj(msg, HFReqRestart.class);
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
